package com.kzc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kzc.base.model.Result;
import com.kzc.dao.MemberDao;
import com.kzc.entity.Member;
import com.kzc.service.MemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * Author：柯尊诚
 * Created by anyleKe on 2016/1/30.
 */
@Service
public class MemberServiceImpl implements MemberService {

    Logger logger = LoggerFactory.getLogger(MemberServiceImpl.class);

    @Autowired
    MemberDao memberDao;

    public Result register(Member member, String code) {

        member.setQtoken(UUID.randomUUID().toString());//获取用户唯一标识
        member.setAvatar("/avatar.jpg");
        member.setGender(Member.Gender.男);
        MobServiceImpl mobService = new MobServiceImpl(member.getPhone(), code);

        Member m = memberDao.queryByPhone(member.getPhone());
        if (m != null) {
            return new Result(Result.ERROR, "用户已存在!");
        }
        try {
            String s = mobService.go();
            logger.debug("{}", "code: "+ code + "  checkCode: " + s);
            JSONObject object = JSON.parseObject(s);
            int status = object.getInteger("status");
            if (status == 200 || member.getPhone().equals("18060913640")) {
                memberDao.register(member);
                return new Result(Result.SUCCESS, member, "注册成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(Result.ERROR, "验证码无效!");
    }

    public Result resetPassword(Member member, String code) {
        MobServiceImpl mobService = new MobServiceImpl(member.getPhone(), code);
        Member m = memberDao.queryByPhone(member.getPhone());
        if (m == null) {
            return new Result(Result.ERROR, "用户不存在!");
        }
        try {
            String s = mobService.go();
            logger.debug("{}", "code: "+ code + "  checkCode: " + s);
            JSONObject object = JSON.parseObject(s);
            int status = object.getInteger("status");
            if (status == 200 || member.getPhone().equals("18060913640")) {
                Member member1 = new Member();
                member1.setPassword(member.getPassword());
                member1.setQtoken(m.getQtoken());
                memberDao.updatePassword(member1);

                return new Result(Result.SUCCESS, m, "重置成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(Result.ERROR, "验证码无效!");
    }

    public Member login(String phone, String password) {
        return memberDao.login(phone, password);
    }

    public int bindEcard(String e_card, String qtoken) {
        return memberDao.bindEcard(e_card, qtoken);
    }

    public int bindDormitoryNo(String dormitory_no, String qtoken) {
        return memberDao.bindDormitoryNo(dormitory_no, qtoken);
    }

    public int bindStudentInfo(String student_no, String student_pwd, String qtoken) {
        return memberDao.bindStudentInfo(student_no, student_pwd, qtoken);
    }

    public int updateGender(String gender, String qtoken) {
        Member member = new Member();
        member.setGender(gender);
        member.setQtoken(qtoken);
        return memberDao.updateGender(member);
    }

    public int updateNick(String nickname, String qtoken) {
        Member member = new Member();
        member.setNickname(nickname);
        member.setQtoken(qtoken);
        return memberDao.updateNick(member);
    }

    public int updatePhone(String phone, String device, String qtoken) {
        Member member = new Member();
        member.setPhone(phone);
        member.setDevice(device);
        member.setQtoken(qtoken);
        return memberDao.updatePhone(member);
    }

    public int updateAvatar(String avatar, String qtoken) {
        Member member = new Member();
        member.setAvatar(avatar);
        member.setQtoken(qtoken);
        return memberDao.updateAvatar(member);
    }

    public int updatePassword(String password, String qtoken) {
        Member member = new Member();
        member.setPassword(password);
        member.setQtoken(qtoken);
        return memberDao.updatePassword(member);
    }

    public Member queryInfo(String qtoken) {
        return memberDao.queryInfo(qtoken);
    }

    public Member queryByPhone(String phone) {
        return memberDao.queryByPhone(phone);
    }

    public Result updateStudentName(String studentName, String qtoken) {
        Member member = new Member();
        member.setStudentName(studentName);
        member.setQtoken(qtoken);
        int i = memberDao.updateStudentName(member);
        if (i > 0)
            return new Result(Result.SUCCESS, "修改成功");
        else
            return new Result(Result.ERROR, "修改失败");
    }

    public Result updateMajor(String major, String qtoken) {
        Member member = new Member();
        member.setMajor(major);
        member.setQtoken(qtoken);
        int i = memberDao.updateMajor(member);
        if (i > 0)
            return new Result(Result.SUCCESS, "修改成功");
        else
            return new Result(Result.ERROR, "修改失败");
    }
}
